package com.example.shellcollector.database;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Transaction;
import androidx.room.Update;

import com.example.shellcollector.Entity.AppDailyUsage;
import com.example.shellcollector.Entity.InstalledAppUsage;
import com.example.shellcollector.Entity.AppUsageWithDailyUsages;

import java.util.List;

@Dao
public interface InstalledAppUsageDao {
    @Insert
    void insertAll(List<InstalledAppUsage> installedAppUsageList);

    @Transaction
    @Query("select * from installed_app_usage")
    List<AppUsageWithDailyUsages> getAppUsagesWithDailyUsages();

    @Transaction
    @Query("select * from installed_app_usage where id = :appId")
    AppUsageWithDailyUsages getAppUsageWithDailyUsages(int appId);

    @Query("select * from app_daily_usage where app_id = :appId order by date")
    List<AppDailyUsage> getAppDailyUsages(int appId);

    @Query("select * from app_daily_usage where app_id = :appId and date >= :startDate and date <= :endDate order by date")
    List<AppDailyUsage> getAppDailyUsagesInRange(int appId, long startDate, long endDate);

    @Query("select (count(id) > 0) from installed_app_usage where app_package_name = :appPackageName")
    boolean isAppInstalled(String appPackageName);

    @Query("select * from installed_app_usage where app_package_name = :appPackageName")
    InstalledAppUsage getAppUsage(String appPackageName);

    @Query("select * from installed_app_usage where id = :id")
    InstalledAppUsage getAppUsage(int id);

    @Update
    void update(List<InstalledAppUsage> installedAppUsages);

    @Query("select * from installed_app_usage order by total_running_time desc")
    List<InstalledAppUsage> getAllInstalledAppUsages();

    @Query("select * from installed_app_usage order by " +
            "CASE when :isAsc = 1 THEN launch_count END asc, " +
            "CASE when :isAsc = 0 THEN launch_count END desc")
    List<InstalledAppUsage> getAllOrderByLaunchCount(boolean isAsc);

    @Query("select * from installed_app_usage order by " +
            "CASE when :isAsc = 1 THEN app_name END asc, " +
            "CASE when :isAsc = 0 THEN app_name END desc")
    List<InstalledAppUsage> getAllOrderByAppName(boolean isAsc);

    @Query("select * from installed_app_usage order by " +
            "CASE when :isAsc = 1 THEN app_package_name END asc, " +
            "CASE when :isAsc = 0 THEN app_package_name END desc")
    List<InstalledAppUsage> getAllOrderByAppPackageName(boolean isAsc);

    @Query("select * from installed_app_usage order by " +
            "CASE when :isAsc = 1 THEN first_running_time END asc, " +
            "CASE when :isAsc = 0 THEN first_running_time END desc")
    List<InstalledAppUsage> getAllOrderByFirstRunningTime(boolean isAsc);

    @Query("select * from installed_app_usage order by " +
            "CASE when :isAsc = 1 THEN last_running_time END asc, " +
            "CASE when :isAsc = 0 THEN last_running_time END desc")
    List<InstalledAppUsage> getAllOrderByLastRunningTime(boolean isAsc);

    @Query("select * from installed_app_usage order by " +
            "CASE when :isAsc = 1 THEN total_running_time END asc, " +
            "CASE when :isAsc = 0 THEN total_running_time END desc")
    List<InstalledAppUsage> getAllOrderByTotalRunningTime(boolean isAsc);

    @Query("select * from installed_app_usage " +
            "where app_name like '%' || :name || '%' order by app_name asc")
    List<InstalledAppUsage> findByName(String name);
}
